package config

import (
	"gorm.io/gorm/logger"
	"strings"
)

type Mysql struct {
	Host        string `json:"host" yaml:"host"`
	Port        string `json:"port" yaml:"port"`
	Username    string `json:"username" yaml:"username"`
	Password    string `json:"password" yaml:"password"`
	DBName      string `json:"dbname" yaml:"dbname"`
	Config      string `json:"config" yaml:"config"`
	MaxIdleConn int    `json:"max_idle_conn" yaml:"MaxIdleConn"`
	MaxOpenConn int    `json:"max_open_conn" yaml:"MaxOpenConn"`
	LogMode     string `json:"log_mode" yaml:"LogMode"`
}

func (m *Mysql) Dsn() string {
	//dsn := "root:ccebdcxy@tcp(127.0.0.1:3306)/shorturl?charset=utf8&parseTime=True&loc=Local"
	return m.Username + ":" + m.Password + "@tcp(" + m.Host + ":" + m.Port + ")/" + m.DBName + "?" + m.Config
}

func (m Mysql) LogLevel() logger.LogLevel {
	switch strings.ToLower(m.LogMode) {
	case "silent", "Silent":
		return logger.Silent
	case "error", "Error":
		return logger.Error
	case "warn", "Warn":
		return logger.Warn
	case "info", "Info":
		return logger.Info
	default:
		return logger.Info
	}
}
